// pages/marketTool/putinthos/putinhos.js
const lbbService = require('../../service/service.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabType: "",
        hosAgentList:[],
        hosMarketerList:[],
        agentPage:1,
        hosMarketPage:1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log("市场信息录入---》", options)
        if(options.tabType) {
            this.setData({
                tabType: options.tabType
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.setData( {
            hosAgentList:[],
            hosMarketerList:[],
            agentPage:1,
            hosMarketPage:1
        })
        this.getPutinHosList()
        this.getPutinAgentList()
    },

    getNextPageData() {
       if(this.data.tabType == "") {
          this.data.hosMarketPage++
          this.getPutinHosList()
       }else if(this.data.tabType == 'agent') {
          this.data.agentPage++
          this.getPutinAgentList()
       }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    /**
     * 去添加代理信息
     */
    toAddAgentInfo() {
        this.toAgentInfo('add')
    },

    /**
     * 去添加投放医院市场信息
     */
    toAddHosMarkter() {
        this.toHosMarketer('add')
    },
    toAgentInfo(type, params) {
        wx.navigateTo({
            url: 'agentInfo/agentInfo?type=' + type + '&' + params,
        })
    },
    toHosMarketer(type, params) {
        wx.navigateTo({
            url: 'hosMarketer/hosMarketer?type=' + type + '&' + params,
        })
    },

    getPutinHosList() {
        let that = this
        lbbService.getPutinHosMarketerList({pageNum:that.data.hosMarketPage}, function (res) {
            console.log("hosMarketerList>>", res)
            if(res.result == 0) {
                if(res.data.list.length > 0) {
                    res.data.list.forEach(element => {
                        that.data.hosMarketerList.push(element)
                    });
                    console.log("--->hosAgentList-->", that.data.hosMarketerList)
                    that.setData({
                        hosMarketerList: that.data.hosMarketerList
                    })
                } else if(res.data.pageNum > 1){
                    that.data.hosMarketPage = res.data.pageNum - 1
                    wx.showToast({
                      title: '已没有数据',
                    })
                }
            }
            
        })
    },

    getPutinAgentList() {
        let that = this
        lbbService.getPutinAgentList({pageNum: that.data.agentPage},function(res) {
            console.log("agentList-->", res)
            if(res.result == 0) {
                if(res.data.list.length > 0) {
                    res.data.list.forEach(element => {
                        that.data.hosAgentList.push(element)
                    });
                    console.log("--->hosAgentList-->", that.data.hosAgentList)
                    that.setData({
                        hosAgentList: that.data.hosAgentList
                    })
                } else if(res.data.pageNum > 1){
                    that.data.agentPage = res.data.pageNum - 1
                    wx.showToast({
                      title: '已没有数据',
                    })
                }
            }
        })
    },
 
    toggleTab: function (e) {
        let type = e.currentTarget.dataset.type;
        console.log(type, this.data.tabTpye)        
        this.setData({
            tabType: type
        });
    },

    toHosMarketDetail: function(e) {
      let id = e.currentTarget.dataset.id;
      this.toHosMarketer('update', 'id=' + id)
    },

    toAgentDetail: function(e) {
      let id = e.currentTarget.dataset.id
      this.toAgentInfo('update', 'id='+id)
    }
})